package com.xdtech.hotel.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.core.init.InitCacheData;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.hotel.service.CardTypeService;
import com.xdtech.hotel.vo.CardTypeItem;
import com.xdtech.web.model.ComboBox;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-04-04 11:10:07
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class CardTypeController {
	@Autowired
	private CardTypeService cardTypeService;
	@RequestMapping(value="/cardType.do",params = "cardType")
	public ModelAndView skipCardType() {
		return new ModelAndView("hotel/cardType/cardType_ftl");
	}
	
	@RequestMapping(value="/cardType.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(HttpServletRequest request,Pagination pg) {
		Map<String, Object> results = null;
		if (StringUtils.isEmpty(request.getParameter("page"))) {
			//不分页处理
			results = cardTypeService.loadPageAndCondition(null, null);
		}else {
			results = cardTypeService.loadPageAndCondition(pg, null);
		}
		
		return results;
	}
	
	@RequestMapping(value="/cardType.do",params = "editCardType")
	public ModelAndView editCardType(HttpServletRequest request,Long cardTypeId) {
		if (cardTypeId!=null) {
			request.setAttribute("cardTypeItem", cardTypeService.loadCardTypeItem(cardTypeId));
		}
		return new ModelAndView("hotel/cardType/editCardType_ftl");
	}
	
	@RequestMapping(value="/cardType.do",params = "saveCardType")
	@ResponseBody
	public ResultMessage saveCardType(CardTypeItem item) {
		ResultMessage r = new ResultMessage();
		if (cardTypeService.saveOrUpdateCardType(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/cardType.do",params = "deleteCardTypeItems")
	@ResponseBody
	public ResultMessage deleteCardTypeItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> cardTypeIds = new ArrayList<Long>();
			for (String id : tempIds) {
				cardTypeIds.add(Long.valueOf(id));
			}
			cardTypeService.deleteCardTypeInfo(cardTypeIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(params = "loadCardTypeList")
	@ResponseBody
	public Object loadCardTypeList(String value) {
		List<ComboBox> comboBoxsTemp = new ArrayList<ComboBox>();
		List<CardTypeItem> cardTypeItems = cardTypeService.loadItems();
		for (CardTypeItem cardTypeItem : cardTypeItems) {
			comboBoxsTemp.add(new ComboBox(cardTypeItem.getCode(), cardTypeItem.getName(), cardTypeItem.getCode().equals(value)));
		}
		return comboBoxsTemp;
	}
}
